$(function(){
	init();	
 
	//方法窗口树形菜单加载
	$('#methodModifyTree').fileTree(
			{
				root: '00', 
				script: 'role.do?type=methodTree'
			},function(file,name,id){

			},function(file,name,id){
				
	});
	
	//方法修改对话框加载
	$("#methodModifyTreeWindow").dialog({
		draggable:false,
		resizable:false,//窗口不可以改变大小
		bgiframe: true,
		autoOpen:false,
		width:screen.width - 300,
		height:400,
		open:function(event,ui)
		{
			$(event.target).width ($(event.target).dialog("option","width "));//可能有不加空格纵向下拉条变窄
			$(event.target).height ($(event.target).dialog("option","height"));
		},
		stack:false,
		modal:true,
		position:'top',
		title:'功能管理架构',
		buttons:{"取消":function(){$("#methodModifyTreeWindow").dialog("close");},"保存":addMethod}
	});
});

function init(){
	$.ajaxSetup({cache:false});
	initGrid();
	initDialog();
}

function initDialog(){
	$("#detailDialog").dialog({
			bgiframe: true,
			autoOpen:false,
			width:310,
			stack:false,
			modal:true,
			position:'center',			
			title:'详细信息',
			buttons:{"取消":function(){$("#detailDialog").dialog("close");},"增加": addPriceItem,"保存":updatePriceItem,"删除":deletePriceItem},
			open:function(){
				$("#detailDialog").parent().find(".ui-dialog-buttonpane button:visible").focus();
			}
			
	});
	
}



function initGrid(){	
	jQuery("#roleList").jqGrid({
	   	url:'role.do?type=list',
	   	height:450,	   	
		datatype: "xml",
	  	
	   	colModel:[
	   	          	{name:'ID', width:100,xmlmap:'ID',align:'center',label:'系统编号'},
	   	          	{name:'name', width:100,xmlmap:'name',label:'名称'}
	   	],
	   	xmlReader:{
	   		repeatitems:false
	   	},
	   	pager: $('#rolePager'),
	   	pgbuttons: false,
		pginput: false,
	   	rowNum:100,
	   	autowidth: true,
	   	sortname: 'id',
	    viewrecords: true,
    	caption:"角色列表",
    	gridComplete:function(){
    		resizeJQGrid({jqGrid:this.id,userData:false});
    	}
	}).navGrid('#rolePager',{edit:false,add:false,del:false,refresh:false,search:false});
	
	if(p_delete){
		jQuery("#roleList").navButtonAdd("#rolePager",{
			caption : "删除",
			title: '删除所选择记录',
			buttonicon : 'ui-icon-trash',
			onClickButton:processDelete,
			position : "first",
			cursor : 'pointer'
		});
	}
	
	if(p_update){
		jQuery("#roleList").navButtonAdd("#rolePager",{
			caption : "编辑",
			title: '编辑所选记录',
			buttonicon : 'ui-icon-pencil',
			onClickButton:processEdit,
			position : "first",
			cursor : 'pointer'
		});
	}
	
	if(p_add){
		jQuery("#roleList").navButtonAdd("#rolePager",{
			caption : "新增",
			title: '增加新记录',
			buttonicon : 'ui-icon-plus',
			onClickButton:processAdd,
			position : "first",
			cursor : 'pointer'
		});
	}

	//methodManage
	if(p_functionMethodRole){
		jQuery("#roleList").navButtonAdd("#rolePager",{
			caption : "功能方法",
			title: '功能方法',
			buttonicon : 'ui-icon-role',
			onClickButton:processMethod,
			position : "first",
			cursor : 'pointer'
		});
	}
}

function initDialogForm(type){
	$("#detailDialog input").each(function(){
		$(this).val("");
	});
	
	if(type == "insert"){
		//$("#detailDialog #ID").removeClass("readonly").removeAttr("readonly","readonly");
		$("#detailDialog #ID").addClass("readonly").attr("readonly","readonly");
	}else if(type == "update"){
		$("#detailDialog #ID").addClass("readonly").attr("readonly","readonly");
	}
	
	
}

function loadingSelectedRowData(){
	 var selectedRowID = $("#roleList").getGridParam("selrow");
	 $.post(
	 	"role.do?type=query",
	 	{
	 		ID:selectedRowID
	 	},
	 	function(xml){
			$("#detailDialog #ID").val( $(xml).find("Role ID").text());
			$("#detailDialog #name").val( $(xml).find("Role name").text());
	 	},
	 	"xml"
	 );
}

function processAdd(){
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('增加')").show();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").hide();
	initDialogForm("insert");
	$("#detailDialog").dialog("open");
}

function processEdit(){
	var selectedRowID = $("#roleList").getGridParam("selrow");
	
	 if(!selectedRowID){
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"roleList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('增加')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('删除')").hide();
	$("#detailDialog").parent().find(".ui-dialog-buttonpane button:contains('保存')").show();
	initDialogForm("update");
	loadingSelectedRowData();
	$("#detailDialog").dialog("open");
}

function processDelete(){
	var selectedRowID = $("#roleList").getGridParam("selrow");
	
	 if(!selectedRowID){
	 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
	 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"roleList",jqm:true}); 
	 		$("#jqg_alrt").focus();
	 		return;
	 }
	 
	if(window.confirm("确认删除")){
		$.post(
		"role.do?type=delete",
		{
			ID:selectedRowID
		},
		function(xml){
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 if(flag === 1){
			 	$("#roleList").delRowData( selectedRowID);
			 	alert("删除成功");
			 	
			 }
			 else{
			 	alert(body);
			 }
						 
		},
		"xml"
		);
	}
}


function validOpertor(){
	var validFlag = true;
	
	$("#detailDialog input:not(.readonly)").each(function(){
		
		if(validFlag && $(this).next("span.required").size()>0  && $.trim($(this).val()).length === 0){
			alert($(this).prev("label").html() + "不能为空");
			$(this).focus();
			validFlag = false;
			return;
		}
	});
	
	return validFlag;
	
}

function addPriceItem(){
	if(validOpertor() === false){
		return;
	}
	$.post(
		"role.do?type=insert",
		{
			name:$("#detailDialog #name").val()
		},
		function(xml){
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 if(flag === 1){
			 	var dataRow = 
			 	{
					ID:body,
					name:$("#detailDialog #name").val()
			 	};
			 	
			 	$("#roleList").addRowData(body,dataRow);
			 	
			 	alert("新增成功");
			 	$("#detailDialog").dialog("close");
			 }else{
			 	alert(body);
			 }
		},
		"xml"
	);
}
function updatePriceItem(){
	if(validOpertor() === false){
		return;
	}
	$.post(
		"role.do?type=update",
		{
			ID:$("#detailDialog #ID").val(),
			name:$("#detailDialog #name").val()
		},
		function(xml){
			 var flag   = parseInt($(xml).find("flag").text());
			 var body = $(xml).find("body").text();
			 if(flag === 1){
			 	var dataRow = 
			 	{
						ID:$("#detailDialog #ID").val(),
						name:$("#detailDialog #name").val()
			 	};
			 	
			 	$("#roleList").setRowData( $("#detailDialog #ID").val(),dataRow);
			 	
			 	alert("更新成功");
			 	$("#detailDialog").dialog("close");
			 } else{
			 	alert(body);
			 }
		},
		"xml"
	);
}

function deletePriceItem(){}

//方法功能对话框加载
function processMethod(){
	//选择一行作为焦点并获取id号
	var selectRow = $("#roleList").getGridParam("selrow");
	
	if(!selectRow){
	 	//警告对话框的初始化
 		var alertIDs = {themodal:'alertmod',modalhead:'alerthd',modalcontent:'alertcnt'};
 		viewModal("#"+alertIDs.themodal,{gbox:"#gbox_"+"priceItemList",jqm:true}); 
 		$("#jqg_alrt").focus();
 		return;
	}
	
	//加载前先清掉勾选的缓存
	$("#uu input").removeAttr("checked");
	
	//弹出修改方法的属性菜单
	$("#methodModifyTreeWindow").dialog("open");
	$("#methodModifyTreeWindow").css("width",screen.width - 305);
	//点击操作后向服务器请求有哪些是已经勾选过的
	$.post(
		"role.do?type=methodIsCheck",
		{
			id:selectRow
		},
		function(xml){	
			var str = $(xml).find("str").text();
			var functionIdStr = $(xml).find("functionId").text();
			var methodIdStr = $(xml).find("methodId").text();
			if(str != "notData"){
				if(functionIdStr != ""){
					functionIdStr = functionIdStr.substring(0,functionIdStr.length - 1);
					functionIdStr = functionIdStr.split(",");
					for(var i=0;i<functionIdStr.length;i=i+1){
						$("#" + functionIdStr[i]).attr("checked","checked");
					}
				}
				
				if(methodIdStr != ""){
					methodIdStr = methodIdStr.substr(0,methodIdStr.length-1);
					methodIdStr = methodIdStr.split(",");//js中字符串拆分数组
					for(var i=0;i<methodIdStr.length;i=i+1){
						$("#" + methodIdStr[i]).attr("checked","checked");
					}
				}
			}
			
			//给功能checkbox增加单击事件
			$(".functionLook").each(function(){				
				$(this).click(function(){
					if(!$(this).attr("checked")){
						$(this).nextAll().removeAttr("checked");
					}
				});
			});
			
			$(".methodLook").each(function(){
				$(this).click(function(){
					if($(this).attr("checked")){
						$("#" + $(this).attr("name")).attr("checked","checked");
					}
				});
			});
		},"xml"
	);
}

//功能方法勾选保存
function addMethod(){
	/**
	 * 方法的id传递
	 */
	var str = "";
	var temp = "";
	$("#uu input:checked").each(function(){
		if($(this).attr("disabled") == false){
			temp = $(this).val();//把当前勾选的id值赋值个变量temp
			str = str + temp + ",";	
		}
	});

	/**
	 * 功能id传递
	 */
	var str1 = "";
	var temp1 = "";
	$(".functionLook:checked").each(function(){
		temp1 = $(this).val().substring(1);//把当前勾选的id值赋值个变量temp
		str1 = str1 + temp1 + ",";
		while(temp1.length > 2){
			str1 = str1 + temp1.substr(0,temp1.length-2) + ",";
			temp1 = temp1.substr(0,temp1.length-2);
		}
		
	});
	var selectRow = $("#roleList").getGridParam("selrow");
	$.post(
		"role.do?type=addMethod",
		{	
			functionStr:str1,
			id:selectRow,
			str:str
		},function(xml){
			 var flag = $(xml).find("flag").text();
			 var body = $(xml).find("body").text();
			 if(flag == 1){		 	
			 	alert("修改成功");
			 	$("#methodModifyTreeWindow").dialog("close");
			 }else{
			 	alert(body);
			 }
		},
		"xml"
	);
}
